<?php

namespace app\common\lists;


trait ListsSearchTrait
{

    protected $params = [];
    protected $searchWhere = [];
    protected $hasWhere = [];


    /**
     * 创建搜索
     * @param $search
     * @param $modelName
     * @return array
     */
    private function createWhere($search, $modelName)
    {
        $where = [];
        $hasWhere = [];
        if (!$search) {
            return [$where, $hasWhere];
        }
        foreach ($search as $whereType => $whereFields) {
            switch ($whereType) {
                case '=':
                case 'eq':
                case 'neq':
                case '<>':
                case '>':
                case '>=':
                case '<':
                case '<=':
                case 'in':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName]) || $this->params[$paramsName] === '') {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, $whereType, trim($this->params[$paramsName])];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, $whereType, trim($this->params[$paramsName])];
                        }
                    }
                    break;
                case 'eq_date':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, '=', strtotime($this->params[$paramsName])];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, '=', strtotime($this->params[$paramsName])];
                        }
                    }
                    break;
                case 'like':
                case '%like%':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, 'like', '%' . trim($this->params[$paramsName]) . '%'];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, 'like', '%' . trim($this->params[$paramsName]) . '%'];
                        }
                    }
                    break;
                case '%like':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, 'like', '%' . trim($this->params[$paramsName])];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, 'like', '%' . trim($this->params[$paramsName])];
                        }
                    }
                    break;
                case 'like%':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, 'like', trim($this->params[$paramsName]) . '%'];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, 'like', trim($this->params[$paramsName]) . '%'];
                        }
                    }
                    break;
                case 'betweenTimestamp':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName][0]) || !isset($this->params[$paramsName][1])) {
                            continue;
                        }
                        if (!$this->params[$paramsName][0] || !$this->params[$paramsName][1]) {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, 'between', [strtotime($this->params[$paramsName][0]), strtotime($this->params[$paramsName][1]) + 86399]];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, 'between', [strtotime($this->params[$paramsName][0]), strtotime($this->params[$paramsName][1]) + 86399]];
                        }
                    }
                    break;
                case 'between':
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName][0]) || !isset($this->params[$paramsName][1])) {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, 'between', [$this->params[$paramsName][0], $this->params[$paramsName][1]]];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, 'between', [$this->params[$paramsName][0], $this->params[$paramsName][1]]];
                        }
                    }
                    break;
                case 'find_in_set': // find_in_set查询
                    foreach ($whereFields as $key => $whereField) {
                        $paramsName = substr_symbol_behind($whereField);
                        if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
                            continue;
                        }
                        if (is_int($key)) {
                            $where[] = [$modelName . $whereField, 'find in set', $this->params[$paramsName]];
                        } else {
                            $keys = explode('.', $key);
                            $prefix = $keys[0];
                            $key = $keys[1];
                            $hasWhere[$prefix][] = [$key, 'find in set', $this->params[$paramsName]];
                        }
                    }
                    break;
                case 'custom': // 自定义查询
                    foreach ($whereFields as $whereField) {
                        $where[] = $whereField;
                    }
                    break;
            }
        }
        return [$where, $hasWhere];
    }
}
